
function createTable(toid,jsondata){
    var table=document.createElement("table");
    table.id="tables"
    let tr=document.createElement("tr");
    tr.id="lastTr";
    let button=document.createElement("button");
    let arr=new Array();
    for(let i=0;i<3;i++){
        let td=document.createElement("td");
        arr[i]=td;
        tr.appendChild(td);
    }
    arr[0].appendChild(document.createTextNode("总金额"));
    arr[1].appendChild(document.createTextNode(0));
    arr[1].id="sums";
    arr[2].appendChild(button);
    button.appendChild(document.createTextNode("提交"))
    for (i in jsondata) {
       let tr = document.createElement("tr");
                   //创建tr
        //________________创建表头________________________________________
        if (i == 0) {
            for (j in jsondata[i]) {                                      //根据数据在tr内创建td
                let th = document.createElement("th");
                th.appendChild(document.createTextNode(jsondata[i][j]));
                tr.appendChild(th);
            }
        }else{
            for (j in jsondata[i]){      
                // console.log(j);                                //根据数据在tr内创建td
                let td = document.createElement("td");
                if(j=="picture"){
                    let img=document.createElement("img");
                    img.setAttribute("src",jsondata[i][j])
                    td.appendChild(img);
                    tr.appendChild(td);
                }else if(j=="number"){
                    let div=document.createElement("div")
                    div.id="sy"
                    let div1=document.createElement("div");
                    div1.id="number";
                    div1.setAttribute("onclick","createInput(this)");
                    let Addbutton=document.createElement("button");
                    Addbutton.id="button2"
                    let Reducebutton=document.createElement('button');
                    Reducebutton.id="button1"
                    Reducebutton.setAttribute("onclick","clickButton(this)")
                    Addbutton.setAttribute("onclick","clickButton(this)")
                    div1.appendChild(document.createTextNode(jsondata[i][j]));
                    Addbutton.appendChild(document.createTextNode("+"));
                    Reducebutton.appendChild(document.createTextNode("-"));
                    td.appendChild(div)
                    div.appendChild(Addbutton);
                    div.insertBefore(div1,Addbutton);
                    div.insertBefore(Reducebutton,div1);
                    tr.appendChild(td);
                }
                else{
                    td.appendChild(document.createTextNode(jsondata[i][j]));
                    tr.appendChild(td);
                }
            }
        }
        table.appendChild(tr);
    }
    document.getElementById(toid).appendChild(table);
    table.appendChild(tr);
}
function createInput(self) {
    let td_text = self.innerHTML;
    let editor = document.createElement("input");
    editor.id="input1"
    editor.setAttribute("type", "text");
    editor.value = td_text;
    editor.onblur = function () {
      let a = this;
          self.innerHTML = a.value;
    };
    editor.onclick = function () {
      event.stopPropagation();
    };
    self.innerHTML = "";
    self.appendChild(editor);
    editor.focus();
  }      
  function Sum(){
    let sums=document.getElementById("sums");
    let arr=new Array();
    let Tr=document.getElementsByTagName("tr");
    let n=0;
    for(let i=0;i<Tr.length-2;i++){
        arr[i]=Number(Tr[i+1].lastChild.innerHTML);
    }
        for(let i=0;i<arr.length;i++){
            n+=arr[i];
        }
        sums.innerHTML=n;
  }
function clickButton(self){
    let sum=self.parentNode.parentNode;
    if(self.innerHTML=='-'){
        if(self.nextSibling.innerHTML>0){
            self.nextSibling.innerHTML--;
            sum.nextSibling.innerHTML=parseInt(sum.previousSibling.innerHTML)*Number(self.nextSibling.innerHTML);
        }else{
            sum.nextSibling.innerHTML=0;
        }
    }else{
        self.previousSibling.innerHTML++;
        sum.nextSibling.innerHTML=parseInt(sum.previousSibling.innerHTML)*Number(self.previousSibling.innerHTML);
    }
    Sum();
}
